﻿using Apps.Common;
using Apps.Models;
using System.Linq;
using System.Collections.Generic;
using Apps.Models.PUB;

namespace Apps.BLL.PUB
{
    public  partial class PUB_OF_NODEBLL
    {

    public override List<PUB_OF_NODEModel> GetListByParentId(ref GridPager pager, string queryStr, object parentId)
    {
        IQueryable<PUB_OF_NODE> queryData = null;
        string pid = parentId.ToString();
        if (pid != "0")
        {
        queryData = m_Rep.GetList(a => a.TACHE_ID == pid);
        }
        else
        {
        queryData = m_Rep.GetList();
        }
        if (!string.IsNullOrWhiteSpace(queryStr))
        {
            queryData = m_Rep.GetList(
                        a => (
                                a.NODE_NAME.Contains(queryStr)
                               || a.TACHE_ID.Contains(queryStr)
                               || a.NODE_ID.Contains(queryStr)
                             )
                        );
        }
        pager.totalRows = queryData.Count();
        queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
        return CreateModelList(ref queryData);
    }

        public   List<PUB_OF_NODEModel> GetListByNodeList(string NodeList)
        {
            IQueryable<PUB_OF_NODE> queryData = null;
            string[] nodes = NodeList.Split(',');
            queryData = m_Rep.GetList(a => nodes.Contains(a.NODE_ID) && a.ENABLED);
            return CreateModelList(ref queryData);
        }

        public string GetListByNodeNameList(string NodeList)
        {
            string strResule = "";
            List<PUB_OF_NODE> queryData = null;
            string[] nodes = NodeList.Split(',');
            queryData = m_Rep.GetList(a => nodes.Contains(a.NODE_ID) && a.ENABLED).ToList();
            foreach (var o in queryData)
            {
                strResule += string.IsNullOrEmpty(strResule) ? o.NODE_NAME : "," + o.NODE_NAME;
            }
            return strResule;
        }


        public List<PUB_OF_NODE> GetGroup(string groupid)
        {
            IQueryable<PUB_OF_NODE> queryData = null;
            queryData = m_Rep.GetList(a => a.ENABLED);

            var whereLambda = PredicateBuilder.True<PUB_OF_NODEModel>();
            whereLambda = whereLambda.And(a => a.TACHE_IDName.Contains("采样"));
            if (!string.IsNullOrEmpty(groupid))
            {
                whereLambda = whereLambda.And(a => a.POSTION == int.Parse(groupid));
            }

            List<PUB_OF_NODEModel> nodes = CreateModelList(ref queryData).Where(whereLambda.Compile()).ToList();
            //遍历子节点
            List<PUB_OF_NODE> list = new List<PUB_OF_NODE>();
            foreach (var item in nodes)
            {
                PUB_OF_NODE node = new PUB_OF_NODE();
                node.NODE_NAME = item.NODE_NAME;
                node.NODE_ID = item.NODE_ID;
                list.Add(node);
            }
            return list;
        }

        public string GetGroupName(string ids)
        {
            return m_Rep.Context.Database.SqlQuery<string>($"SELECT FUN_GROUPNAME('" + ids + "') FROM DUAL").FirstOrDefault();
        }


        public override List<PUB_OF_NODEModel> CreateModelList(ref IQueryable<PUB_OF_NODE> queryData)
        {

            List<PUB_OF_NODEModel> modelList = (from r in queryData
                                              select new PUB_OF_NODEModel
                                              {
                                                  NODE_CODE = r.NODE_CODE,
                                                  POSTION = r.POSTION,
                                                  ENABLED = r.ENABLED,
                                                  NODE_NAME = r.NODE_NAME,
                                                  TACHE_ID = r.TACHE_ID,
                                                  NODE_ID = r.NODE_ID,
                                                  TACHE_IDName=r.PUB_OF_TACHE.TACHE_NAME
                                              }).ToList();
            return modelList;
        }
    }
 }

